import 'package:cbec_app/components/base/base_chekbox.dart';
import 'package:cbec_app/components/base/base_interactive.dart';
import 'package:cbec_app/components/base/base_page.dart';
import 'package:cbec_app/components/base/base_nav_bar.dart';
import 'package:cbec_app/components/common/common_style.dart';
import 'package:cbec_app/components/common/not_found.dart';
import 'package:cbec_app/components/goods/goods_info_item.dart';
import 'package:cbec_app/pages/my/controller/collect_ctr.dart';
import 'package:cbec_app/utils/Tools.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';
import 'package:left_scroll_actions/leftScroll.dart';
import 'package:left_scroll_actions/left_scroll_actions.dart';

//收藏
class Collect extends GetView<CollectController> {
  const Collect({super.key});

  @override
  Widget build(BuildContext context) {
    return GetBuilder<CollectController>(builder: (controller){
      return BasePage(
        title: "我的收藏",
        iconType: IconType.back,
        isShowPadding: true,
        children: [
          Container(
            margin: EdgeInsets.only(top: 14.w),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.end,
              children: [
                
                controller.data.isNotEmpty? BaseInteractive(
                  onClick: () => {controller.edit()},
                  childWidget: Text(controller.actionTxt,
                      style: setTextStyle(15, FontWeight.w500,
                          Tools.fromHex("#333333"))),
                ):emptyWidget()
              ],
            )),

          SizedBox(
            height: 570.h,
            child: controller.data.isNotEmpty? Tools.getPullRefresh(
              refreshController: controller.baseRefreshCtr,
              onLoading: controller.onPageLoading,
              onRefresh: controller.onPageRefresh,
              enablePullDown: true,
              enablePullUp: controller.hasNextPage,
              child: ListView.builder(
                  itemCount: controller.data.length,
                  itemBuilder: (context, index) {
                    final Map<String,dynamic> item = controller.data[index];
                    item['goodsId'] = item['commodityId'];
                    return LeftScroll(
                      key: Key(item['goodsId'].toString()),
                      closeTag: LeftScrollCloseTag(item['goodsId'].toString()),
                      buttons: [
                        deleteBox(item['goodsId'], index)
                      ],
                      child: GoodsInfoItem(
                                isShowCheckBox: controller.showButton,
                                infoStyle: InfoStyle.collect,
                                btnType:  RightBtnType.car,
                                rightBtnCarClick: () {
                                  controller.addCar(item['goodsId'].toString());
                                },
                                margin: EdgeInsets.only(top: 10.w),
                                goodsInfo: item,
                                //{"goodsId":item["commodityId"], "mainImage":item['mainImage'],"commodityName":"测试","price":item["price"],"priceMarket":"100","isSelect":item['isSelect']}
                                checkboxCallback: (groupIndex,index,selected){
                                  // print("zhe:$groupIndex, ${item['goodsId']} ,选中状态:$selected");
                                  controller.choise(item['commodityId'], selected);
                                },
                              ),
                    );
                  })):const NotFound(),
          ),

          Positioned(
            key: GlobalKey(debugLabel: 'Positioned'),
            bottom: MediaQuery.of(context).padding.bottom,
            child: Visibility(
                visible: controller.showButton,
                child: Container(
                  width: 375.w,
                  color: Colors.white,
                  padding: EdgeInsets.fromLTRB(18.w, 10.w, 18.w, 10.w),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Row(
                        children: [
                          BaseCheckBox(
                              value: controller.selectedAll,
                              onChange: (selected) {
                                controller.selectedAllItem(selected);
                              },
                            ),
                          SizedBox(width: 10.w),
                          Text("全选",
                              style: setTextStyle(12, FontWeight.w500,
                                  Tools.fromHex("#333333")))
                        ],
                      ),
                      Row(
                        children: [
                          solidButton(
                              "取消收藏 (${controller.choiseNum})",
                              radius: 30,
                              width: 90.w,
                              backgroundColor: Tools.fromHex("#F83F2D"),
                              () {
                                controller.delete();
                              })
                        ],
                      )
                    ],
                  ),
                )))
          
        ],
      );
    });
  }


  Widget deleteBox(dynamic id,int index){
    return Container(
      padding: EdgeInsets.only(top: 15.w,bottom: 5.w),
      child: LeftScrollItem(
      color: Colors.red,
          text: '删除',
          onTap: () {
            Tools.showModal(content: "是否取消收藏?" ,onCancel:(tag) {
              
            },onConfirm: (tag) {
              controller.deleteOne(id, index);
              SmartDialog.dismiss(tag: tag);
            },);
          },
        ),
    );
  }
}




